package net.lab1024.smartadmin.module.business.tourist.controller;

import net.lab1024.smartadmin.common.anno.NoNeedLogin;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.tourist.domain.dto.TouristAddDTO;
import net.lab1024.smartadmin.module.business.tourist.domain.dto.TouristUpdateDTO;
import net.lab1024.smartadmin.module.business.tourist.domain.dto.TouristQueryDTO;
import net.lab1024.smartadmin.module.business.tourist.domain.vo.TouristVO;
import net.lab1024.smartadmin.module.business.tourist.domain.vo.TouristExcelVO;
import net.lab1024.smartadmin.module.business.tourist.service.TouristService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * [  ]
 *
 * @author wldd
 * @version 1.0
 * @company 游客
 * @copyright (c)  游客Inc. All rights reserved.
 * @date 2020-12-28 19:40:01
 * @since JDK1.8
 */
@RestController
@Api(tags = {""})
public class TouristController extends BaseController {

    @Autowired
    private TouristService touristService;

    @ApiOperation(value = "分页查询",notes = "@author wldd")
    @PostMapping("/tourist/page/query")
    public ResponseDTO<PageResultDTO<TouristVO>> queryByPage(@RequestBody TouristQueryDTO queryDTO) {
        return touristService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "根据code添加旅客信息",notes = "@author wldd")
    @GetMapping("/tourist/addTouristByCode")
    @NoNeedLogin
    public ResponseDTO<String> addTouristByCode(@RequestParam(value = "code") String code) {
        return touristService.addTouristByCode(code);
    }

    @ApiOperation(value = "添加",notes = "@author wldd")
    @PostMapping("/tourist/add")
    public ResponseDTO<String> add(@RequestBody @Validated TouristAddDTO addTO){
        return touristService.add(addTO);
    }

    @ApiOperation(value="修改",notes = "@author wldd")
    @PostMapping("/tourist/update")
    public ResponseDTO<String> update(@RequestBody @Validated TouristUpdateDTO updateDTO){
        return touristService.update(updateDTO);
    }

    @ApiOperation(value="批量删除",notes = "@author wldd")
    @PostMapping("/tourist/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return touristService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author wldd")
    @PostMapping("/tourist/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<TouristExcelVO> touristList = touristService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TouristExcelVO.class, touristList);
        downloadExcel("", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author wldd")
    @PostMapping("/tourist/export/all")
    public void exportAll(@RequestBody @Validated TouristQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<TouristExcelVO> touristList = touristService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TouristExcelVO.class, touristList);
        downloadExcel("", workbook, response);
    }

}
